package top.kaoshanji.p2ch16a;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 代码清单-p2ch16a-475页b：表示账户的类
 * author kaoshanji
 * ^_^
 * create time 2025/2/26 16:53
 */
public class Account {

    private Lock lock = new ReentrantLock();
    private volatile double money;

    public Account(double initialMoney) {
        this.money = initialMoney;
    }

    public void add(double money) {
        lock.lock();
        try {
            this.money += money;
        } finally {
            lock.unlock();
        }
    }

    public void reduce(double money) {
        lock.lock();
        try {
            this.money -= money;
        } finally {
            lock.unlock();
        }
    }










    public double getMoney() {
        return money;
    }

    void lock() {
        lock.lock();
    }

    void unLock() {
        lock.unlock();
    }

    boolean tryLock() {
        return lock.tryLock();
    }


}
